﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using Castle.Core;
using Castle.Core.Resource;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;

namespace SportStore.WebUI
{
    public class WindsorControllerFactory : DefaultControllerFactory
    {
        private WindsorContainer container;

        public  WindsorControllerFactory()
        {
            container = new WindsorContainer(new XmlInterpreter(new ConfigResource("castle")));
            var controllerType = from t in Assembly.GetExecutingAssembly().GetTypes()
                                 where typeof (IController).IsAssignableFrom(t)
                                 select t;

            foreach (Type t in controllerType)
            {
                container.AddComponentLifeStyle(t.FullName, t, LifestyleType.Transient);
                
            }
        }

        protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
        {
            if (controllerType == null) return null;
            return (IController)container.Resolve(controllerType);
            //return base.GetControllerInstance(requestContext, controllerType);
        }

    }
}